package adv8;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XMLRead {
	// 建立读取器，根据路径获得文档对象，获得根节点
	public static void main(String[] args) throws DocumentException {
		SAXReader reader = new SAXReader();
		Document doc = reader.read(new File("students2.xml"));
		Element root = doc.getRootElement();
		System.out.println(root.getName() + " 是根节点");
		// 利用xpath来查找节点
		// root.selectNodes("stduent");
		List<Element> list = doc.selectNodes("/stduents/student");
		for (Element e : list) {
			String no = e.attributeValue("kgcno");
			String sex = e.attributeValue("sex");
			// Element name=(Element) e.selectNodes("name").get(0);
			Element name = (Element) e.selectSingleNode("name");
			String xingming = name.getText();
			System.out.println("姓名：" + xingming);
			System.out.println("课工场注册号是：" + no + ",性别=" + sex);
			System.out.println("qq:" + e.selectSingleNode("qq").getText());
			System.out.println("telephone:" + e.selectSingleNode("telephone").getText());
			System.out.println("school:" + e.selectSingleNode("school").getText());
			System.out.println("addr:" + e.selectSingleNode("addr").getText());

			System.out.println("???????????????????????");
		}
	}
}
